<%@page import="wap.rockvilletech.portal.DBProc"%>
<%@ page import="java.io.*,java.util.*,java.sql.*,java.text.*"%>

<%@ page import="org.apache.log4j.Logger" %>

<% final Logger log = Logger.getLogger("JSP.WEATHER");%>
<%@include file="includes/header.jspf" %>
<style type="text/css">.temperature {font-size:120%; padding: 5px 0; }
.condition {font-weight: bold; padding-right: 10px; }
.summary {font-size: 85%; padding-bottom: 5px; }
.forecast {margin-top: 5px; border: 1px solid black; text-align: center; }
.forecast th {background-color: black; width: 15%; color: white;}
.fiveday-icons td {padding-bottom: 5px; }
.fiveday-icons div {padding-top: 30px; background-position: 50% 0; background-repeat: no-repeat; font-weight: bold; }
.divWeather {text-align: left; }
.weatherImage {padding-right: 5px; padding-bottom: 2px; vertical-align: middle; }
.weatherHeading {font-weight: bold; text-decoration: underline; padding-top: 5px; }
.tbl_forecast {text-align: center; }
#wiff {background:transparent url(http://l.yimg.com/a/lib/ywc/img/wicons.png) repeat scroll 0 0;}</style>
<%!    DBProc dbp = DBProc.getInstance();

    public String horizontlToVertical(String tbl) {
        String strReturn = "<table class=\"tbl_forecast\"><tbody>";
        try {
            String rootTable = tbl;
            //String rootTable="<tbody><tr><th>Today</th><th>Tomorrow</th><th>Sun</th><th>Mon</th><th>Tue</th></tr><tr><td><div class=\"iconDiv\" style=\"background-image: url(http://l.yimg.com/a/i/us/nws/weather/gr/34s.png);\">Mostly Sunny</div></td><td><div class=\"iconDiv\" style=\"background-image: url(http://l.yimg.com/a/i/us/nws/weather/gr/30s.png);\">Partly Cloudy</div></td><td><div class=\"iconDiv\" style=\"background-image: url(http://l.yimg.com/a/i/us/nws/weather/gr/34s.png);\">Mostly Sunny</div></td><td><div class=\"iconDiv\" style=\"background-image: url(http://l.yimg.com/a/i/us/nws/weather/gr/30s.png);\">Partly Cloudy</div></td><td><div class=\"iconDiv\" style=\"background-image: url(http://l.yimg.com/a/i/us/nws/weather/gr/30s.png);\">Partly Cloudy</div></td></tr><tr><td>High: 27 <div>Low: 13</div></td><td>High: 28 <div>Low: 12</div></td><td>High: 30 <div>Low: 13</div></td><td>High: 29 <div>Low: 13</div></td><td>High: 27 <div>Low: 14</div></td></tr></tbody>";
            //log.debug("RootTable\n"+rootTable);
            String strInTable = rootTable.substring(rootTable.indexOf("<table") + 28, rootTable.indexOf("</table>")).toString();
            //log.debug("StringTable\n"+strInTable);
            strInTable = strInTable.replaceAll("th>", "td>");
            strInTable = strInTable.replaceAll("<tr(\\s)*class(.|\\s)*?>", "<tr>");
            //strInTable = strInTable.replaceAll("<tr class=\"fiveday-icons\"", "<tr");

            //log.debug("StringTable after <TH tag Replacement>\n"+strInTable);
            String row = "<tr>";
            String col = "<td>";

            //String tempRow;
            String tempCol = "";

            // int rowCounter,colCounter=0;

            String[] trs = strInTable.split(row);

            String swapStr;
            //log.debug("Rows Length" + trs.length);

            for (int indexr = 1; indexr < trs.length; indexr++) {
                swapStr = trs[indexr];
                trs[indexr] = swapStr.replaceFirst("</tr>", "");
                //log.debug("Rows"+trs[indexr]);
                tempCol += trs[indexr].toString();
                swapStr = "";
            }
            //log.debug("TempCol"+tempCol);

            String[] cols = tempCol.split(col);
            String classIcon = "<td class=\"fiveday-icons\"";
            String classtemp = "<td class=\"fiveday-temps\"";
            for (int c = 1; c < cols.length; c++) {
                cols[c] = "<td>" + cols[c];
                //log.debug("Column "+c+" "+cols[c]);
                if (c > 0 && c < 6) {
                    cols[c] = cols[c].replace("td>", "th>");
                    //log.debug("Column "+c+" "+cols[c]);
                }
                if (c > 5 && c < 11) {
                    cols[c] = cols[c].replace("<td", classIcon);
                    // log.debug("Column "+c+" "+cols[c]);
                }
                if (c > 10 && c < cols.length) {
                    cols[c] = cols[c].replace("<td", classtemp);
                    //log.debug("Column after 10 \n"+c+" "+cols[c]);
                }
            }

            String[] tcl = new String[cols.length];

            tcl[1] = "<tr>" + cols[1];
            tcl[2] = cols[6];
            tcl[3] = cols[11];
            tcl[4] = "</tr><tr>" + cols[2];
            tcl[5] = cols[7];
            tcl[6] = cols[12];
            tcl[7] = "</tr><tr>" + cols[3];
            tcl[8] = cols[8];
            tcl[9] = cols[13];
            tcl[10] = "</tr><tr>" + cols[4];
            tcl[11] = cols[9];
            tcl[12] = cols[14];
            tcl[13] = "</tr><tr>" + cols[5];
            tcl[14] = cols[10];
            tcl[15] = cols[15];
            tcl[15] = tcl[15] + "</tr>";

            for (int v = 1; v < tcl.length; v++) {
                strReturn += tcl[v].toString();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return strReturn + "</tbody></table>";
    }
%>

<%
            thisPage="weather.jsp";
           //-- String app = "", id = "", CatID = "", pid = "", bid = "", width = "200", ua = "";
            String city = "", feedURL = "";
            String ititle = "", idesc = "";

            String strCity = "";
            String strSubmitVal = "";

            boolean isSummary = false;




            feedURL = request.getParameter("feed") != null ? request.getParameter("feed").toString() : "";
            city = request.getParameter("city") != null ? request.getParameter("city").toString() : "";
            width = request.getParameter("w") != null ? request.getParameter("w").toString() : width;
            strSubmitVal = request.getParameter("submit") != null ? request.getParameter("submit").toString() : "";

            app = request.getParameter("app") != null ? request.getParameter("app").toString() : "";
            app=city;
           /* CatID = request.getParameter("CatID") != null ? request.getParameter("CatID").toString() : "";
            pid = request.getParameter("pid") != null ? request.getParameter("pid").toString() : "";
            bid = request.getParameter("bid") != null ? request.getParameter("bid").toString() : "";
*/

            log.debug("Weather.jsp " + feedURL + ", " + city + "," + width + ", " + strSubmitVal + ", ");

            if (width == null || width.equalsIgnoreCase("") || width.equalsIgnoreCase("null")) {
                width = "200";
            }            
            if (city.length() < 2) {
                    feedURL = "Summary";
                    city = "Summary";
                } else {
                    feedURL = city;
                }
            
            if (feedURL.contains("Summary")) {
                isSummary = true;

            }


%>
<div style="padding: 0px 0px 0px;">
<div class="div_navigation" id="div_navigation">
    <a href="<%=request.getContextPath()%>?page=index.jsp" class="LinkIndex">Home</a>&nbsp;&gt;
	<span class="navigation_Leaf">Weather</span>
</div>
</div>
    <div>

         <table width="100%" align="left" cellpadding="0" cellspacing="0" style="text-align: left;font-weight: bold;">
        <tr>
            <td style="background-image:url('images/new/top_bar_02.jpg');background-repeat: repeat-x;height:26px;background-color:#D7D5D5;">
                <b style="padding:0px 0px 0px 2px;">&nbsp;Weather&nbsp;</b>
            </td>
        </tr>
    </table>
     </div>
        <div align="right">
    <form method="post" action="<%=request.getContextPath()%>/weather.jsp">

        <select name="city">
            <%
                        Vector vCities = dbp.getWeatherCities();
                        for (int i = 0; i < vCities.size(); i++) {
                            strCity = vCities.get(i).toString();
                            if (strCity.equals("International")) {%>
            <optgroup label="<%=strCity%>"></optgroup><%
                                        } else {%>
            <option value="<%=strCity%>" <%=(city.equalsIgnoreCase(strCity) ? "selected" : "")%> ><%=strCity%></option><%
                            }
                        }
            %>
        </select>
        <input type="submit" value="GO" name="submit" />
    </form>
</div>

<%

            if (null != feedURL && (!feedURL.equals("")) && feedURL.length() > 0) {
                try {
                    Vector vWeather = dbp.getWeatherDetail(city);
                    ititle = vWeather.get(1).toString();
                    ititle = wap.rockvilletech.util.StringUtils.toUnicode(ititle);

                    idesc = vWeather.get(2).toString();
                    idesc = wap.rockvilletech.util.StringUtils.toUnicode(idesc);
                    //log.debug("idesc: "+idesc);
                    idesc = wap.rockvilletech.util.StringHelper.unescapeHTML(idesc);
                    //idesc=idesc.replaceAll("<p>","<p style=\"margin:2px 0 0 0;\">");

                    try {
                        idesc = java.net.URLDecoder.decode(idesc, "UTF-8");
                    } catch (Exception exDecode) {
                    }

                    try {
                        ititle = java.net.URLDecoder.decode(ititle, "UTF-8");
                    } catch (Exception exDecode) {
                    }

                    if (idesc.toLowerCase().contains("feedflare")) {
                        idesc = idesc.substring(0, idesc.indexOf("<div class=\"feedflare"));
                    }
                    // Convert Table Horizontal to Vertical w.r.t Width
                    if (!isSummary && Integer.parseInt(width) < 320) {
                        String rootStr, beforeTable, tableString, afterTable;

                        rootStr = idesc;
                        String startIndex = "<table class=\"tbl_forecast\">";
                        beforeTable = rootStr.substring(0, rootStr.indexOf(startIndex));
                        //log.debug("Before table string"+beforeTable);
                        tableString = rootStr.substring(rootStr.indexOf(startIndex), rootStr.lastIndexOf("/table>") + 7);
                        //log.debug("Table string"+tableString);
                        afterTable = rootStr.substring(rootStr.lastIndexOf("/table>") + 7);
                        // log.debug("After table"+afterTable);
                        tableString = this.horizontlToVertical(tableString).toString();
                        idesc = beforeTable + tableString + afterTable;
                    }
%>

<div style="text-align: left;margin-left:3px; ">
    <div id="feedHeading" class="feedHeading"><%=ititle%></div>
    <div id="feedDescription" class="feedDescription"><%=idesc%></div>
</div>
<%
                } catch (Exception e) {
                    log.error("Weather, Currently not available - feedURL :" + feedURL + " City :" + city, e);
                    out.println("Weather Not Available!");
                }
            } // feedURL check
            else {
%><div id="newsBlock" id="newsBlock">Weather, Currently not available</div><%                    }
%>


<%@ include file="includes/others.jspf"%>
    <div style="padding:0px 0px 0px 0px;text-decoration: underline;">
        <jsp:include page="banner.jsp">
            <jsp:param name="w" value="<%=width%>"/>
        </jsp:include>
    </div>
    
<%@include file="includes/footer.jspf" %>

<%
    wpb.LogVisit(strIP, thisPage, strGsm, strApn, strBrand, strModel, app, request.getQueryString());
%>